<?php
class BackgroudSound extends DataObject{
	static $db = array (
		"Name" => "Varchar(255)",
		"Status"=>"Boolean",
	);
	static $searchable_fields = array (
		'Name'=> array('title'=>'Tên'),
		'Status'=> array('title'=>'Trạng thái'),
	);
	/**
	 * Overload so that the default sort.
	 */
	static $summary_fields = array (
		'Name' => 'Tên',
	);
	static $has_one = array (
		"File"=>"File"
		);
		function getCMSFields($controller = null) {
			$fileUpload = new FileIFrameField( 'File', 'Nhạc nền' );
			$fileUpload->allowedExtensions = array("mp3", "wav");
			$file_validator = new Upload_Validator();
			$file_validator->setAllowedExtensions(array("mp3", "wav"));
			$fileUpload->setValidator($file_validator);
			$ArticleTabVn= new Tab(_t('Support.TABCONTENTTITLEVN', 'Cài đặt nhạc nền'),
			new TextField("Name", _t('Support.Name', 'Tên')),
			$fileUpload,
			new CheckboxField("Status",_t('Support.Status', 'Hoạt động'))
			);
			$fields = new FieldSet(new TabSet("Root",$ArticleTabVn));
			return $fields;
		}
		function getCMSValidator() {
			return new RequiredFields(array('Name'));
		}
		public function getSound()
		{
			$output="";
			$sqlQuery = new SQLQuery();
			$sqlQuery->select = array(
			"b.ID as ID",
	     	"b.Name as Nam",
			"File.FileName as File",
			);
			$sqlQuery->from = array("BackgroudSound b","LEFT JOIN File ON b.FileID = File.ID");
			$sqlQuery->where = array("b.Status=1");
			$sqlQuery->limit = "1";
			$sqlQuery->orderby("b.Created DESC");
			// get the raw SQL
			$rawSQL = $sqlQuery->sql();
			$result = $sqlQuery->execute();
			$filepath="";
			$name="";
			if($result)
			{
				foreach($result as $row) {
					$filepath=$row["File"];
				}
			}
			if($filepath)	{
				$output.="<script language='javascript'>";
				$output .="if ( navigator.appName == 'Netscape' ) {";
				$output .="document.write( '<embed src = \"".$filepath."\" autostart=\"true\" hidden=\"true\" loop=\"true\" width=\"0\" height=\"0\"></embed>' );";
				$output .="} else if ( navigator.appName == 'Microsoft Internet Explorer' ) {";
				$output .="document.write( '<bgsound src = \"".$filepath."\" loop=\"-1\" width=\"0\" height=\"0\">' );";
				$output .="} else document.write( '<bgsound src = \"".$filepath."\" loop=\"-1\" width=\"0\" height=\"0\"><embed src = \"".$filepath."\" autostart=\"true\" hidden=\"true\" loop=\"true\" width=\"0\" height=\"0\"></embed>' );";
				$output.="</script>";
			}
			return $output;
		}
		function canCreate() {return true;}
		function canEdit() {return true;}
		function canDelete() {return true;}
}
?>